
let workerObj: Worker;

// 错误监听
export function onError(e) {
  console.log(e);
}
// 消息接收
export function onMessage(e) {
  console.log(e);
}
// 发送的数据无法序列化成字符串时，会触发这个事件
export function onMessageError(e) {
  console.log(e);
}
// 发送消息
export function sendMessage(param) {
  workerObj?.postMessage(param);
}
// 关闭线程-当前正在执行的任务不会停止
export function closeFn() {
  self.close();
}
// 终止线程-停止所有正执行的任务
export function stopFn() {
  workerObj.terminate();
}
// 初始化线程
export function workerInit() {
  if (window.Worker) {
    workerObj = new Worker('ceshi.js');
    workerObj.onmessage = onMessage;
    workerObj.onerror = onError;
    workerObj.onmessageerror = onMessageError;
  }
}
